﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using Util_XLS;
using 聚欣订单分析.Ling;

namespace 聚欣订单分析
{
    public partial class DataImport : System.Web.UI.Page
    {
        const string UploadDirectory = "~/UploadControl/";
        public string strInfo = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                seYear.Value = DateTime.Now.Year;
                Page.Title = " 订单分析系统";
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string strFile = ucFile.FileName;
            string fileExt = "";//文件扩展名
            string newFile = MapPath(UploadDirectory) + "mydd.xlsx";

            if (strFile == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('请选择文件!')</script>");
                return;

            }

            fileExt = strFile.Substring(strFile.LastIndexOf(".") + 1);
            if (fileExt.ToLower() != "xlsx")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('文件扩展名不对!')</script>");
                return;
            }
            beginProgress("ProgressBar.htm");
            setProgress("正在上传");
            if (File.Exists(newFile))
            {
                File.Delete(newFile);
            }
            ucFile.PostedFile.SaveAs(newFile);
            strInfo = "数据库上传成功！";
            finishProgress();
        }

        private void Drs(string strUrl, string strTableName)
        {
            beginProgress("ProgressBar.htm");
            setProgress("正在同步");
            using (up_GeneralDataContext Gdc = new up_GeneralDataContext())
            {
                Gdc.up_GeneralDeleteProc("tbl_DDB", "年份=" + seYear.Value.ToString());
            }
            strInfo = "";
            tbl_DDBDataContext DDB = new tbl_DDBDataContext();
            string sql = String.Format("select top 1000 * from {0}' where 订单号<>''", ReadXls.ConvertToSQLSheetName(strTableName));
            DataTable dt = ReadXls.SelectFromXLS(strUrl, sql).Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["订单号"].ToString().Trim() != "")
                {
                    string srtID = seYear.Value.ToString() + (i + 1).ToString();
                    string strHkeq = dt.Rows[i]["第一笔回款日期"].ToString();
                    if (dt.Rows[i]["第二笔回款日期"].ToString().Trim() != "")
                    {
                        strHkeq = dt.Rows[i]["第二笔回款日期"].ToString().Trim();
                    }
                    string strValues = "'" + srtID + "','" + seYear.Value.ToString() + "','" + dt.Rows[i]["订单号"].ToString() + "','" + dt.Rows[i]["省"].ToString() + "','" + dt.Rows[i]["地市"].ToString() + "','" + dt.Rows[i]["运营商（简写）"].ToString() + "','" + dt.Rows[i]["项目"].ToString() + "','" + dt.Rows[i]["类型"].ToString() + "','" + dt.Rows[i]["有无订单合同"].ToString() + "','" + dt.Rows[i]["订单日期"].ToString() + "','" + NumericNull(dt.Rows[i]["订单金额"].ToString()) + "','" + dt.Rows[i]["发货"].ToString() + "','" + dt.Rows[i]["发货"].ToString() + "','" + dt.Rows[i]["结束货期"].ToString() + "','" + dt.Rows[i]["开票"].ToString() + "','" + dt.Rows[i]["开票时间"].ToString().Trim() + "','" + NumericNull(dt.Rows[i]["开票金额"].ToString()) + "','" + strHkeq + "','" + NumericNull(dt.Rows[i]["回款金额"].ToString()) + "','" + dt.Rows[i]["第二笔回款日期"].ToString() + "','" + NumericNull(dt.Rows[i]["回款金额1"].ToString()) + "','" + NumericNull(dt.Rows[i]["尾款"].ToString()) + "','" + dt.Rows[i]["责任人"].ToString() + "','" + dt.Rows[i]["合同签订时间"].ToString() + "','" + dt.Rows[i]["付款方式"].ToString() + "','" + dt.Rows[i]["报账联系人"].ToString() + "','" + dt.Rows[i]["订单取消日期"].ToString() + "'";
                    using (up_GeneralDataContext Gdc = new up_GeneralDataContext())
                    {
                        Gdc.up_GeneralInsertProc("tbl_DDB", "", strValues);
                    }

                }
                else
                {
                    strInfo += "第" + (i + 1).ToString() + "行," + "订单号为空<br/>";
                }
            }
            strInfo += "同步成功！";
            finishProgress();
        }

        /// <summary>
        /// 判断金额是否为空
        /// </summary>
        /// <param name="strNumeric"></param>
        /// <returns></returns>
        private string NumericNull(string strNumeric)
        {
            string strReturn = "0";
            if (strNumeric.Trim() != "")
            {
                strReturn = strNumeric.Trim();
            }
            return strReturn;
        }


        protected void btnRead_Click(object sender, EventArgs e)
        {
            string newFile = MapPath(UploadDirectory) + "mydd.xlsx";
            string strSeetName = txtTableName.Text.Trim();

            if (strSeetName == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('请填写导入表名!')</script>");
                return;
            }

            Drs(newFile, strSeetName);
        }

        private void beginProgress(string strHTML)
        {
            //根据ProgressBar.htm显示进度条界面
            string templateFileName = Path.Combine(Server.MapPath("/"), strHTML);
            StreamReader reader = new StreamReader(@templateFileName, System.Text.Encoding.GetEncoding("GB2312"));
            string html = reader.ReadToEnd();
            reader.Close();
            Response.Write(html);
            Response.Flush();
        }

        private void finishProgress()
        {
            string jsBlock = "<script>SetCompleted();</script>";
            Response.Write(jsBlock);
            Response.Flush();
        }

        private void setProgress(string percent)
        {
            string jsBlock = "<script>SetPorgressBar('" + percent + "'); </script>";
            Response.Write(jsBlock);
            Response.Flush();
        }
    }
}
